#! /usr/bin/python

'''a hand is ranked by the following way:
    0 High Card: Highest value card.
    1 One Pair: Two cards of the same value.
    2 Two Pairs: Two different pairs.
    3 Three of a Kind: Three cards of the same value.
    4 Straight: All cards are consecutive values.
    5 Flush: All cards of the same suit.
    6 Full House: Three of a kind and a pair.
    7 Four of a Kind: Four cards of the same value.
    8 Straight Flush: All cards are consecutive values of same suit.
    * Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
    
    pl1 and pl2 is two list of five tuple represent two player\'s hand in the format below:
    pl1: [(5,\'H\'),(5,\'C\'),(6,\'S\'),(7,\'S\'),(13,\'D\')]
    pl1: [(2,\'C\'),(3,\'S\'),(8,\'S\'),(8,\'D\'),(10,\'D\')]
    each tuple in list is consisted by a number and a char which 11 standard fo J(ack), 12 standard for Q(ueen)
    which player 1 got a pair of fives
    which player 2 got a pair of eights
'''
mapp = {'T':10,
        'J':11,
        'Q':12,
        'K':13,
        'A':14,
        '9':9,
        '8':8,
        '7':7,
        '6':6,
        '5':5,
        '4':4,
        '3':3,
        '2':2,
        '1':1,
        }
def isflush(l):
    return reduce(lambda x,y: x and l[0][1]==y[1], l, True)
def isstraight(l):
    return reduce(lambda x,y: (x[0] and x[1]+1 == y[0],y[0]), l, (True,l[0][0]-1))[0]
def cmpcard(x,y):
    return cmp(x[0],y[0])
def gethands(l):
    return [(mapp[s[0]],s[1]) for s in l[:5]],[(mapp[s[0]],s[1]) for s in l[5:]]
def referer((pl1,pl2)):
    '''return 1 if pl1 win the game, -1 if pl2 win, 0 if draw
    '''
    pl1.sort(cmpcard)
    pl2.sort(cmpcard)
    r1 = rank(pl1)
    r2 = rank(pl2)
    if r1[0] > r2[0]:
        return 1
    elif r1[0] < r2[0]:
        return -1
    else:
        if r1[1] > r2[1]:
            return 1
        elif r1[1] < r2[1]:
            return -1
        else:
            return 0
def rank(l):
    #the rank value, r[0] is the rank0-8, r[1] is the attatched data to judge
    r = [0,None]
    if isstraight(l):
        r[1] = l[-1][0]
        if isflush(l):
            r[0] = 8
        else:
            r[0] = 4
    elif isflush(l):
        r[0] = 5
        r[1] = [card[0] for card in l]
    else:
        #group the list
        g = {}
        nums = [card[0] for card in l]
        for num in nums:
            if g.has_key(num):
                g[num] += 1
            else:
                g[num] = 1
        #keys
        r[1] = g.keys()
        r[1].sort(key=lambda x:g[x]*20+x,reverse=True)
        if len(g) == 2:
            #full house or four
            if g[r[1][0]]==4:
                #four
                r[0] = 7
            else:
                #full
                r[0] = 6
        elif len(g) == 3:
            #two pairs or Three of kind
            if g[r[1][0]]==3:
                #Three
                r[0] = 3
            else:
                #two pair
                r[0] = 2
        elif len(g) == 4:
            #one pair
            r[0] = 1
        else:
            r[0] = 0
    return r
                
def main():
    total = 0
    for line in open('poker.txt'):
        if referer(gethands(line.split())) == 1:
            total += 1
    print total

if __name__ == '__main__':
    main()

